package ty.tryagainforever1129.pikachumemory.common;
import java.util.ArrayList;
import ty.tryagainforever1129.pikachumemory.R;
import ty.tryagainforever1129.pikachumemory.model.UserModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;

public class Util {
    public static void onClickCustom(final View v) {
        v.setVisibility(View.INVISIBLE);
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.setVisibility(View.VISIBLE);
            }
        }, Constant.ITEM_LAYOUT_LIGHT_SPEED_TIME);
    }

    /*public static AlertDialog.Builder dialogQuestion(final Context context, final DialogInterface.OnClickListener
     * okClick, DialogInterface.OnClickListener cancelClick) { AlertDialog.Builder dialog = new
     * AlertDialog.Builder(context); dialog.setCustomTitle(null);
     * dialog.setMessage(R.string.ty_alertdialog_message_tryagain); dialog.setView(view) if (okClick != null) {
     * dialog.setPositiveButton(context.getString(android.R.string.ok), okClick); } if (cancelClick != null) {
     * dialog.setNegativeButton(context.getString(android.R.string.cancel), cancelClick); } else {
     * dialog.setNegativeButton(context.getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
     * @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); } return dialog; } */
    public static boolean getSoundStatus(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Constant.PRE_SETTING_SOUND_ENABLE, false);
    }
    public static String getHeroName(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.PRE_SETTING_USER_NAME,
                context.getString(R.string.blank));
    }
    public static void setHeroName(Context context, String value) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putString(Constant.PRE_SETTING_USER_NAME, value).commit();
    }
    public static int getMinScore(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getInt(Constant.PRE_LEADER_BOARDS_MIN_SCORE, Constant.COMMON_1);
    }
    public static void setMinScore(Context context, int value) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(Constant.PRE_LEADER_BOARDS_MIN_SCORE, value).commit();
    }

    public static void configForTheFirstTime(Context context) {
        // check and config for the first time.
        SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(context);
        if (!pre.getBoolean(Constant.PRE_CONFIG_THE_FIRST_TIME, false)) {
            pre.edit().putBoolean(Constant.PRE_SETTING_SOUND_ENABLE, true).putBoolean(Constant.PRE_CONFIG_THE_FIRST_TIME, true)
                    .commit();
        }
    }

    public static ArrayList<UserModel> getUserModelList(Context context) {
        ArrayList<UserModel> userList = new ArrayList<UserModel>();
        try {
            SharedPreferences pre = context.getSharedPreferences(Constant.PRE_USER_MODEL_LIST, Context.MODE_PRIVATE);
            int counter = pre.getInt(Constant.PRE_USER_MODEL_COUNTER, Constant.COMMON_0);
            UserModel user;
            // check if data is null.
            if (counter == Constant.COMMON_0) {
                // set default data.
                String[] data = Constant.LEADER_BOARDS_DATA.split(Constant.LEADER_BOARDS_DATA_SPLIT_CHAR);
                int loop = data.length / Constant.COMMON_2;
                for (int i = 0; i < loop; i++) {
                    user = new UserModel();
                    user.setUserName(data[i]);
                    user.setScrore(Integer.parseInt(data[i + loop]));
                    userList.add(user);
                }
                // save to database.
                addUserModelList(context, userList);
            } else {
                String strDefault = context.getString(R.string.blank);
                for (int i = 0; i < counter; i++) {
                    // get user data.
                    user = new UserModel();
                    user.setUserName(pre.getString(Constant.PRE_USER_NAME_LIST + i, strDefault));
                    user.setScrore(pre.getInt(Constant.PRE_SCORE_LIST + i, Constant.COMMON_0));
                    // add to list.
                    userList.add(user);
                }
            }
        } catch (NumberFormatException e) {
            Log.i(Constant.TAG_LOGIC,
                    "get user model list with error when set score for user (Number format exception).");
            e.printStackTrace();
        }
        return userList;
    }

    private static void addUserModelList(Context context, ArrayList<UserModel> userList) {
        Editor pre = context.getSharedPreferences(Constant.PRE_USER_MODEL_LIST, Context.MODE_PRIVATE).edit();
        // clear old data.
        pre.clear().commit();
        // save new data.
        for (int i = 0; i < userList.size(); i++) {
            pre.putString(Constant.PRE_USER_NAME_LIST + i, userList.get(i).getUserName());
            pre.putInt(Constant.PRE_SCORE_LIST + i, userList.get(i).getScrore());
        }
        pre.putInt(Constant.PRE_USER_MODEL_COUNTER, userList.size());
        pre.commit();
    }

    public static void addUserModelList(Context context, UserModel newUser) {
        // get old data.
        ArrayList<UserModel> userList = getUserModelList(context);
        // userList.add(newUser);
        userList = addHeroToList(context, userList, newUser);
        Editor pre = context.getSharedPreferences(Constant.PRE_USER_MODEL_LIST, Context.MODE_PRIVATE).edit();
        // clear old data.
        
        pre.clear().commit();
        // save new data.
        for (int i = 0; i < userList.size(); i++) {
            pre.putString(Constant.PRE_USER_NAME_LIST + i, userList.get(i).getUserName());
            pre.putInt(Constant.PRE_SCORE_LIST + i, userList.get(i).getScrore());
        }
        pre.putInt(Constant.PRE_USER_MODEL_COUNTER, userList.size());
        pre.commit();
    }

    private static ArrayList<UserModel> addHeroToList(Context context,ArrayList<UserModel> userList, UserModel user) {
        ArrayList<UserModel> newList = userList;
        for (int i = 0; i < userList.size(); i++) {
            if(user.getScrore() > newList.get(i).getScrore()) {                
                newList.add(i,user);
                break;
            }
        }
        // reset min score.
        setMinScore(context, newList.get(userList.size() - Constant.COMMON_2).getScrore());
        // remove item min score.
        newList.remove(userList.size() - Constant.COMMON_1);
        return newList;
    }
}
